package N2024.M11.D30;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2025-02-04
 * Time: 23:22
 */
public class test2 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue=new ArrayBlockingQueue(10);
        Thread thread1=new Thread(new Producer(queue));
        Thread thread2=new Thread(new Consumer(queue));
        thread1.start();
        thread2.start();
    }
}
//消费者
class Consumer implements Runnable{
    BlockingQueue<Integer> queue;

    Consumer(BlockingQueue<Integer> queue){
        this.queue=queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                int s = queue.take();
                System.out.println(s);
                if (s==9){
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
//生产者
class Producer implements Runnable{
    BlockingQueue<Integer> queue;

    Producer(BlockingQueue<Integer> queue){
        this.queue=queue;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            try {
                queue.put(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}